#!/bin/bash
# Copyright 2019 The SQLFlow Authors. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


set -e

# TRAVIS_PULL_REQUEST is set to the pull request number if the current job is a pull request build, or false if it’s not.
echo "TRAVIS_PULL_REQUEST $TRAVIS_PULL_REQUEST"
# TRAVIS_BRANCH:
# - for push builds, or builds not triggered by a pull request, this is the name of the branch.
# - for builds triggered by a pull request this is the name of the branch targeted by the pull request.
# - for builds triggered by a tag, this is the same as the name of the tag (TRAVIS_TAG).
echo "TRAVIS_BRANCH $TRAVIS_BRANCH"

if [[ "$TRAVIS_PULL_REQUEST" != "false" ]]; then
    echo "skip deployment on pull request"
    exit 0
fi

if [[ "$TRAVIS_BRANCH" == "develop" ]]; then
    # push coverage status to coveralls.io when commit to develop branch
    # "coverage.out" should be generated by previous testing steps.
    if [[ "$TRAVIS_EVENT_TYPE" != "cron" ]]; then
        docker run --rm -e COVERALLS_TOKEN=$COVERALLS_TOKEN -v $GOPATH:/go sqlflow:latest bash -c "cd /go/src/sqlflow.org/sqlflow && /usr/local/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN"
    fi
else
    echo "Not build on develop branch, skip pushing to coveralls.io"
fi

